package com.molyfun.parents.activity.msg;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.fragment.dialog.MsgSignUpDialogFragment;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.rest.bean.GiveUpInformResponse;
import com.molyfun.parents.rest.bean.SignUpInformResponse;
import com.molyfun.parents.utils.Const;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MsgDetailActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.content_title)
    TextView mContentTitle;
    @InjectView(R.id.content)
    TextView mContent;
    @InjectView(R.id.sign_up_organization_name)
    TextView mOrganizationName;
    @InjectView(R.id.sign_up_date)
    TextView mEnrolDate;
    @InjectView(R.id.give_up_button)
    TextView mGiveUpButton;
    @InjectView(R.id.pay_the_fee_button)
    TextView mPayButton;
    @InjectView(R.id.attention_content)
    TextView mAttentionContent;
//    @InjectView(R.id.view_item)
//    View mView_item;
    @InjectView(R.id.layout_option)
    LinearLayout layout_option;
    @InjectView(R.id.base_title)
    TextView mBaseTitle;

    private MsgSignUpDialogFragment.Builder builder;
    private MsgSignUpDialogFragment msgSignUpDialogFragment;
    private MsgSignUpDialogFragment.Builder builderGiveUp;
    private MsgSignUpDialogFragment msgSignUpDialogFragmentGiveUp;
    private String detailId;
    private String departname;
    private String informtime;
    private String mMsgId;
    private String mContentText;
    private String mInform;

    private Handler mHandler = new Handler();
    private String abandonbulletin;
    private String signUpNotice;
    static final String TZ_CODE = "TZ_CODE";

    public static void startActivity(Context context, String detailId, String departname, String informtime, String content, String informe, String msgId) {
        Intent intent = new Intent();
        intent.putExtra(Const.DETAIL_ID, detailId);
//        intent.putExtra(PRODUCT_ID, product_id);
        intent.putExtra("departname", departname);
        intent.putExtra("informtime", informtime);
        intent.putExtra("content", content);
        intent.putExtra(TZ_CODE, informe);
        intent.putExtra("msgId", msgId);
        intent.setClass(context, MsgDetailActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_msg_detail);
        ButterKnife.inject(this);
        initView();

    }

    private void initView() {
        getSupportActionBar().setTitle("");
        detailId = getIntent().getStringExtra(Const.DETAIL_ID);
        departname = getIntent().getStringExtra("departname");
        informtime = getIntent().getStringExtra("informtime");
        mMsgId = getIntent().getStringExtra("msgId");
        mContentText = getIntent().getStringExtra("content");
        mInform = getIntent().getStringExtra("TZ_CODE");
        if (!TextUtils.isEmpty(mInform) && mInform.equals("RXSB")) {
            mContentTitle.setText("入学失败");
            mContent.setText(mContentText);
            layout_option.setVisibility(View.GONE);
//            mView_item.setVisibility(View.GONE);
        } else if (!TextUtils.isEmpty(mInform) && mInform.equals("RXTZ")) {
            mContentTitle.setText("入学通知");
            layout_option.setVisibility(View.GONE);
//            mView_item.setVisibility(View.GONE);
            //TODO 请求接口，查看是否放弃入学
            isGiveUp(detailId);
        }

        if (!TextUtils.isEmpty(mContentText)) {
            mContent.setText(mContentText);
        } else {
            mContent.setText("暂无入学通知");
        }
        mBack.setOnClickListener(this);
        mGiveUpButton.setOnClickListener(this);
        mPayButton.setOnClickListener(this);

        mBaseTitle.setText("消息详情");

        mOrganizationName.setText(departname);
        mEnrolDate.setText(informtime);
        mPayButton.setText("注意事项");
        msgSignUpDialogFragment = new MsgSignUpDialogFragment(this);
        builder = new MsgSignUpDialogFragment.Builder(this);
        msgSignUpDialogFragmentGiveUp = new MsgSignUpDialogFragment(this);
        builderGiveUp = new MsgSignUpDialogFragment.Builder(this);
    }

    private void isGiveUp(final String detailId) {
        Call<GiveUpInformResponse> responseCall = MFPApplication.getInstance().getRestClient().signUpisGiveUp(detailId);
        responseCall.enqueue(new Callback<GiveUpInformResponse>() {
            @Override
            public void onResponse(Call<GiveUpInformResponse> call, Response<GiveUpInformResponse> response) {
                if (response.body() != null && response.body().getMsg() != null) {
                    if (response.body().getData() == 1) {
                        layout_option.setVisibility(View.VISIBLE);
//                        mView_item.setVisibility(View.VISIBLE);
                        mPayButton.setVisibility(View.GONE);
                        mGiveUpButton.setText("已放弃");
                        mGiveUpButton.setClickable(false);
                    } else {
                        layout_option.setVisibility(View.VISIBLE);
//                        mView_item.setVisibility(View.VISIBLE);
                        getSignUpInform(detailId, false,false);
                    }
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<GiveUpInformResponse> call, Throwable t) {
                toast("请求失败");
            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.give_up_button:
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getSignUpInform(detailId, true,true);
                    }
                }, 800);
                break;
            case R.id.pay_the_fee_button:
                //TODO 缴费弹窗
                mHandler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        getSignUpInform(detailId, false,true);
                    }
                }, 800);
                break;
        }

    }

    private String attention = "";

    private void getSignUpInform(String product_id, final boolean flage, final boolean signupFlage) {
        showProgressDialog();
        Call<SignUpInformResponse> signUpInformResponseCall = MFPApplication.getInstance().getRestClient().signUpInform(product_id);
        signUpInformResponseCall.enqueue(new Callback<SignUpInformResponse>() {
            @Override
            public void onResponse(Call<SignUpInformResponse> call, Response<SignUpInformResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getData() != null) {
                    attention = response.body().getData().getAttention();
                    abandonbulletin = response.body().getData().getAbandonbulletin();
                    //TODO 显示入学通知详情
                    signUpNotice = response.body().getData().getNotice();
                    mAttentionContent.setText(signUpNotice);
                    if (flage && signupFlage) {
                        if (!TextUtils.isEmpty(abandonbulletin)) {
                            showGiveUpDialog(abandonbulletin, "放弃", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (!selectorflage) {
                                        submitGiveupInfo(detailId);
                                    } else {
                                        toast("请勾选同意放弃");
                                    }

                                }
                            }, "放弃提示");
                        } else {
                            showGiveUpDialog("放弃本次入学机会，将无法参加本学校的学习，如需再次入学则要再次报名并等待学校通知。", "放弃", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    if (!selectorflage) {
                                        submitGiveupInfo(detailId);
                                    } else {
                                        toast("请勾选同意放弃");
                                    }

                                }
                            }, "放弃提示");

//                            toast("放弃提示内容为空");
                        }
                    } else if(signupFlage){
                        if (!TextUtils.isEmpty(attention)) {
                            showTwiceButtonDialog(attention, "确定", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    msgSignUpDialogFragment.dismiss();
                                }
                            }, "入学注意事项");
                        } else {
                            toast("入学注意事项内容为空");
                        }
                    }else {

                    }
                } else {
                    toast(response.body() == null ? "请求异常" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<SignUpInformResponse> call, Throwable t) {
                dismissProgressDialog();

            }
        });
    }

    private void submitGiveupInfo(String product_id) {
        showProgressDialog();
        Call<BaseResponse> signUpInformResponseCall = MFPApplication.getInstance().getRestClient().signUpGiveupReason(product_id);
        signUpInformResponseCall.enqueue(new Callback<BaseResponse>() {
            @Override
            public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                dismissProgressDialog();
                if (response.body() != null && response.body().getMsg() != null) {
//                    toast(response.body().getMsg());
                    builderGiveUp.getSubmitView().setBackgroundColor(getResources().getColor(R.color.color_split_up));
                    msgSignUpDialogFragmentGiveUp.dismiss();
                    mPayButton.setVisibility(View.GONE);
                    mGiveUpButton.setText("已放弃");
//                    mGiveUpButton.setBackgroundColor(getResources().getColor(R.color.color_split_up));
                    mGiveUpButton.setClickable(false);
                } else {
                    toast(response.body() == null ? "" : response.body().getMsg() == null ? "" : response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseResponse> call, Throwable t) {
                dismissProgressDialog();
                toast("提交失败");
            }
        });
    }

    private void showTwiceButtonDialog(String alertText, String btnText, View.OnClickListener onClickListener, String titletext) {
        msgSignUpDialogFragment = builder.setMessage(alertText).setSingleButton(btnText, onClickListener).showtitleText(titletext).hitCheckIcon().createSingleButtonDialog();
        msgSignUpDialogFragment.show();
    }

    private boolean selectorflage = true;

    private void showGiveUpDialog(String alertText, String btnText, View.OnClickListener onClickListener, String titletext) {
        msgSignUpDialogFragmentGiveUp = builderGiveUp.setMessage(alertText).setSingleButton(btnText, onClickListener).showtitleText(titletext).createSingleButtonDialog();
        msgSignUpDialogFragmentGiveUp.show();
        builderGiveUp.getSubmitView().setBackgroundResource(R.drawable.bg_e06769);
        builderGiveUp.getSubmitView().setTextColor(ContextCompat.getColor(MsgDetailActivity.this, R.color.color_white));
        builderGiveUp.getIconView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (selectorflage) {
                    builderGiveUp.getSubmitView().setBackgroundResource(R.drawable.bg_e94649);
                    builderGiveUp.getIconView().setImageResource(R.mipmap.ic_checkbox_checked);
                    selectorflage = false;
                } else {
                    builderGiveUp.getSubmitView().setBackgroundResource(R.drawable.bg_e06769);
                    builderGiveUp.getIconView().setImageResource(R.mipmap.ic_checkbox_unchecked);
                    selectorflage = true;
                }
            }
        });
    }

}

